<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="合同编号" prop="contNum">
      <el-input v-model="dataForm.contNum" placeholder="合同编号"></el-input>
    </el-form-item>
    <el-form-item label="买方" prop="buyer">
      <el-input v-model="dataForm.buyer" placeholder="买方"></el-input>
    </el-form-item>
    <el-form-item label="卖方" prop="seller">
      <el-input v-model="dataForm.seller" placeholder="卖方"></el-input>
    </el-form-item>
    <el-form-item label="供应商编号" prop="supplierNum">
      <el-input v-model="dataForm.supplierNum" placeholder="供应商编号"></el-input>
    </el-form-item>
    <el-form-item label="签定时间" prop="contDate">
      <el-input v-model="dataForm.contDate" placeholder="签定时间"></el-input>
    </el-form-item>
    <el-form-item label="合同类型" prop="contType">
      <el-input v-model="dataForm.contType" placeholder="合同类型"></el-input>
    </el-form-item>
    <el-form-item label="采购类型" prop="stockType">
      <el-input v-model="dataForm.stockType" placeholder="采购类型"></el-input>
    </el-form-item>
    <el-form-item label="合同标的物" prop="contItem">
      <el-input v-model="dataForm.contItem" placeholder="合同标的物"></el-input>
    </el-form-item>
    <el-form-item label="币别" prop="money">
      <el-input v-model="dataForm.money" placeholder="币别"></el-input>
    </el-form-item>
    <el-form-item label="税率" prop="taxRate">
      <el-input v-model="dataForm.taxRate" placeholder="税率"></el-input>
    </el-form-item>
    <el-form-item label="包装物是否回收" prop="wrapBack">
      <el-input v-model="dataForm.wrapBack" placeholder="包装物是否回收"></el-input>
    </el-form-item>
    <el-form-item label="验收标准" prop="verifyStand">
      <el-input v-model="dataForm.verifyStand" placeholder="验收标准"></el-input>
    </el-form-item>
    <el-form-item label="运输方式" prop="mode">
      <el-input v-model="dataForm.mode" placeholder="运输方式"></el-input>
    </el-form-item>
    <el-form-item label="付款条件" prop="payCond">
      <el-input v-model="dataForm.payCond" placeholder="付款条件"></el-input>
    </el-form-item>
    <el-form-item label="验收时间" prop="verifyDate">
      <el-input v-model="dataForm.verifyDate" placeholder="验收时间"></el-input>
    </el-form-item>
    <el-form-item label="质量要求及技术标准" prop="qualityStand">
      <el-input v-model="dataForm.qualityStand" placeholder="质量要求及技术标准"></el-input>
    </el-form-item>
    <el-form-item label="标准号" prop="standardNo">
      <el-input v-model="dataForm.standardNo" placeholder="标准号"></el-input>
    </el-form-item>
    <el-form-item label="交(提)货地点" prop="place">
      <el-input v-model="dataForm.place" placeholder="交(提)货地点"></el-input>
    </el-form-item>
    <el-form-item label="合同总金额(元)" prop="contPrice">
      <el-input v-model="dataForm.contPrice" placeholder="合同总金额(元)"></el-input>
    </el-form-item>
    <el-form-item label="运输费用负担" prop="transport">
      <el-input v-model="dataForm.transport" placeholder="运输费用负担"></el-input>
    </el-form-item>
    <el-form-item label="付款方式" prop="payWay">
      <el-input v-model="dataForm.payWay" placeholder="付款方式"></el-input>
    </el-form-item>
    <el-form-item label="逾期千分比" prop="outPerm">
      <el-input v-model="dataForm.outPerm" placeholder="逾期千分比"></el-input>
    </el-form-item>
    <el-form-item label="逾期天数" prop="outDay">
      <el-input v-model="dataForm.outDay" placeholder="逾期天数"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          contNum: '',
          buyer: '',
          seller: '',
          supplierNum: '',
          contDate: '',
          contType: '',
          stockType: '',
          contItem: '',
          money: '',
          taxRate: '',
          wrapBack: '',
          verifyStand: '',
          mode: '',
          payCond: '',
          verifyDate: '',
          qualityStand: '',
          standardNo: '',
          place: '',
          contPrice: '',
          transport: '',
          payWay: '',
          outPerm: '',
          outDay: '',
          remark: ''
        },
        dataRule: {
          contNum: [
            { required: true, message: '合同编号不能为空', trigger: 'blur' }
          ],
          buyer: [
            { required: true, message: '买方不能为空', trigger: 'blur' }
          ],
          seller: [
            { required: true, message: '卖方不能为空', trigger: 'blur' }
          ],
          supplierNum: [
            { required: true, message: '供应商编号不能为空', trigger: 'blur' }
          ],
          contDate: [
            { required: true, message: '签定时间不能为空', trigger: 'blur' }
          ],
          contType: [
            { required: true, message: '合同类型不能为空', trigger: 'blur' }
          ],
          stockType: [
            { required: true, message: '采购类型不能为空', trigger: 'blur' }
          ],
          contItem: [
            { required: true, message: '合同标的物不能为空', trigger: 'blur' }
          ],
          money: [
            { required: true, message: '币别不能为空', trigger: 'blur' }
          ],
          taxRate: [
            { required: true, message: '税率不能为空', trigger: 'blur' }
          ],
          wrapBack: [
            { required: true, message: '包装物是否回收不能为空', trigger: 'blur' }
          ],
          verifyStand: [
            { required: true, message: '验收标准不能为空', trigger: 'blur' }
          ],
          mode: [
            { required: true, message: '运输方式不能为空', trigger: 'blur' }
          ],
          payCond: [
            { required: true, message: '付款条件不能为空', trigger: 'blur' }
          ],
          verifyDate: [
            { required: true, message: '验收时间不能为空', trigger: 'blur' }
          ],
          qualityStand: [
            { required: true, message: '质量要求及技术标准不能为空', trigger: 'blur' }
          ],
          standardNo: [
            { required: true, message: '标准号不能为空', trigger: 'blur' }
          ],
          place: [
            { required: true, message: '交(提)货地点不能为空', trigger: 'blur' }
          ],
          contPrice: [
            { required: true, message: '合同总金额(元)不能为空', trigger: 'blur' }
          ],
          transport: [
            { required: true, message: '运输费用负担不能为空', trigger: 'blur' }
          ],
          payWay: [
            { required: true, message: '付款方式不能为空', trigger: 'blur' }
          ],
          outPerm: [
            { required: true, message: '逾期千分比不能为空', trigger: 'blur' }
          ],
          outDay: [
            { required: true, message: '逾期天数不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/contract/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.contNum = data.contract.contNum
                this.dataForm.buyer = data.contract.buyer
                this.dataForm.seller = data.contract.seller
                this.dataForm.supplierNum = data.contract.supplierNum
                this.dataForm.contDate = data.contract.contDate
                this.dataForm.contType = data.contract.contType
                this.dataForm.stockType = data.contract.stockType
                this.dataForm.contItem = data.contract.contItem
                this.dataForm.money = data.contract.money
                this.dataForm.taxRate = data.contract.taxRate
                this.dataForm.wrapBack = data.contract.wrapBack
                this.dataForm.verifyStand = data.contract.verifyStand
                this.dataForm.mode = data.contract.mode
                this.dataForm.payCond = data.contract.payCond
                this.dataForm.verifyDate = data.contract.verifyDate
                this.dataForm.qualityStand = data.contract.qualityStand
                this.dataForm.standardNo = data.contract.standardNo
                this.dataForm.place = data.contract.place
                this.dataForm.contPrice = data.contract.contPrice
                this.dataForm.transport = data.contract.transport
                this.dataForm.payWay = data.contract.payWay
                this.dataForm.outPerm = data.contract.outPerm
                this.dataForm.outDay = data.contract.outDay
                this.dataForm.remark = data.contract.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/contract/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'contNum': this.dataForm.contNum,
                'buyer': this.dataForm.buyer,
                'seller': this.dataForm.seller,
                'supplierNum': this.dataForm.supplierNum,
                'contDate': this.dataForm.contDate,
                'contType': this.dataForm.contType,
                'stockType': this.dataForm.stockType,
                'contItem': this.dataForm.contItem,
                'money': this.dataForm.money,
                'taxRate': this.dataForm.taxRate,
                'wrapBack': this.dataForm.wrapBack,
                'verifyStand': this.dataForm.verifyStand,
                'mode': this.dataForm.mode,
                'payCond': this.dataForm.payCond,
                'verifyDate': this.dataForm.verifyDate,
                'qualityStand': this.dataForm.qualityStand,
                'standardNo': this.dataForm.standardNo,
                'place': this.dataForm.place,
                'contPrice': this.dataForm.contPrice,
                'transport': this.dataForm.transport,
                'payWay': this.dataForm.payWay,
                'outPerm': this.dataForm.outPerm,
                'outDay': this.dataForm.outDay,
                'remark': this.dataForm.remark
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
